package com.dongdongshop.aop;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
@Aspect  //表示当前类是一个切面类
public class LogAOP {

    private Logger logger = LoggerFactory.getLogger(LogAOP.class);

    //切点和通知
    @Pointcut("execution(public * com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.aop.DongKELog)")
    public void pointcut(){

    }

    @Before("pointcut()")
    public void aopBefore(JoinPoint joinPoint){
        //前置通知打印参数
        Object[] args = joinPoint.getArgs();
        logger.info("正在调用 {} 方法 , 参数为 {}",joinPoint.getSignature(),args);
    }

    @AfterReturning(value = "pointcut()",returning = "obj")
    public void aopAfter(Object obj){
        logger.info("方法调用成功 , 返回的结果为: {}", JSONObject.toJSONString(obj));
    }
}
